package cn.wikiflyer.beikang.act;

import java.lang.reflect.Type;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.setting.bean.WKVersionBean;
import cn.wikiflyer.beikang.bean.AdBeanResult;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKCheckAppUpdate;
import cn.wk.libs4a.utils.WKCheckAppUpdate.CheckAppUpdateBack;
import cn.wk.libs4a.utils.WKCheckAppUpdate.GetDataForUpdate;
import cn.wk.libs4a.utils.WKUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.message.PushAgent;

/**
 * 闪屏 implements CheckAppUpdateBack
 */
public class SplashActivity extends BaseActivity implements CheckAppUpdateBack {

	boolean isFirstOpen = false;

	private ImageView img_ad;

	private boolean openAD = false;

	/**
	 * 是否打开推送
	 */
	private void pushOpen() {
		PushAgent mPushAgent = PushAgent.getInstance(ctx);
		mPushAgent.onAppStart();
		if (app().sp.getBoolean("push", false)) {
			mPushAgent.enable();
		} else {
			mPushAgent.disable();
		}
	}

	@Override
	public void init() {
		img_ad = (ImageView) findViewById(R.id.imageView_ad);
		// delayStart();
		pushOpen();
		WKCheckAppUpdate.getInstance().setCheckUpdateback(this);
		WKCheckAppUpdate.getInstance().check(this, new GetDataForUpdate() {
			public String getData() {
				return app().net.get_version();
			}
		});

		app().sp.setString("lazy", "hello");
		checkIsClearData();
		System.out.println("lazy :" + app().sp.getString("lazy"));
	}

	// 检测是否需要清空信息
	private void checkIsClearData() {
		try {
			int saveCode = Integer.valueOf(app().sp.getString(
					"bk_app_ver_code", "0"));
			int curCode = WKUtils.getVersionCode(this);
			// 需要清空,首次安装新版本
			if (saveCode == 0 || saveCode != 0 && saveCode < curCode) {
				// 清空
				app().sp.clear();
				System.out.println("lazy clear data");
			}
			// 保存
			app().sp.setString("bk_app_ver_code", WKUtils.getVersionCode(this)
					+ "");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_splash);
	}

	@Override
	public void setData() {
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 延迟进入引导页或首页
	 */
	private void delayStart() {
		new Handler().postDelayed(new Runnable() {
			public void run() {
				if (isFirstOpen) {
					Intent i = new Intent(SplashActivity.this, GuideAct.class);
					startActivity(i);
				} else {
					Intent i = new Intent(SplashActivity.this, TabAct.class);
					startActivity(i);
				}
			}
		}, 1000);
	}

	/**
	 * 获取广告线程
	 */
	class GetAdTask extends WKAsyncTaskPro {
		public GetAdTask(Context context) {
			super(context, true, false);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.getAd(params);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<AdBeanResult>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<AdBeanResult> base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				if (openAD)
					app().imageLoader.displayImage(
							base.data.list.get(0).ad_image, img_ad);
			} else {
			}
			delayStart();
		}
	}

	@Override
	public void checkBack(String result) {
		Log.e("wj", "result:" + result);
		Type cvbType = new TypeToken<BKBaseBean<WKVersionBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<WKVersionBean> base = gson.fromJson(result, cvbType);
		if (base.status == 1) {
			int curVer = WKUtils.getVersionCode(activity);
			if (base.data.ver_num > curVer) {
				showDownloadDialog(base.data.url, base.data.ver_desc);
			} else {
				// AlertDialog.Builder builder = new Builder(activity);
				// builder.setTitle("更新提示");
				// builder.setMessage("已经是最新版本了! ");
				// builder.show();
				delayStart();
			}
		} else {
			Toast.makeText(ctx, "访问服务器出错", Toast.LENGTH_SHORT).show();
			delayStart();
		}
	}

	/**
	 * 弹出提示下载对话框
	 */
	public void showDownloadDialog(final String downloadUrl, String msg) {
		AlertDialog.Builder builder = new Builder(activity);
		builder.setTitle("提示");
		builder.setMessage(Html.fromHtml(msg));
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						app().sp.clear();// 确定更新就要清除缓存数据
						Uri uri = Uri.parse(downloadUrl);
						Intent intent = new Intent(Intent.ACTION_VIEW, uri);
						activity.startActivity(intent);
						dialog.dismiss();
						finish();
					}
				});
		builder.setNeutralButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						delayStart();
					}
				});
		builder.create().show();
	}
}
